package com.guass.nettyprotostuff.rpc;

import com.guass.nettyprotostuff.rpc.common.ModelMsg;
import com.guass.nettyprotostuff.rpc.utils.ProtostuffUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;


/**
 * @author guass
 */
public class MsgDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) {
        try {

            byte[] body = new byte[in.readableBytes()];
            in.readBytes(body);

            list.add(ProtostuffUtils.deserialize(body, ModelMsg.class));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}